#include "ennemis.h"
#include "mymainwindow.h"

#include<QDebug>

Ennemis::Ennemis(float t, QString d, float v, float h, float r):QGraphicsItem(),taille(t),dep(d),vitesse(v),hp(h),resist(r),mort(false),arrive(false){
    monimage=&monimage1;
    largeur=taille*(600.0/16);

}


void Ennemis::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){

    painter->drawPixmap(0,0,200,200, *monimage);
}


QRectF Ennemis::boundingRect() const
{
    return QRectF(0,0,600,600);

}

QPainterPath Ennemis::shape() const{
    QPainterPath path;
    path.addRect(0,0,taille*600.0/16,taille*600.0/16);
    return path;

}

void Ennemis::changerImage(){
    if (monimage==&monimage1)
        monimage=&monimage2;
    else monimage=&monimage1;
}

void Ennemis::setPosition(float x, float y){
    if(rotation()==0)
        this->setPos(x-largeur/2,y-largeur/2);
    else if(rotation()==90)
        this->setPos(x+largeur/2,y-largeur/2);
    else if(rotation()==-90)
        this->setPos(x-largeur/2,y+largeur/2);
    else if(rotation()==180)
        this->setPos(x-largeur/2,y-largeur/2);
}


void Ennemis::deplacer(){
    static int j;
    static int i;


    //sur quelle case est le milieu : cas particulier pour chaque rotation
    //calcul de j
    if(rotation()==0 || rotation()==-90 )
        j = (x()+largeur/2)/((float)600/16);
    else
        j = (x()-largeur/2)/((float)600/16);

    //calcul de i
    if(rotation()==0 || rotation()==90 )
        i = (y()+largeur/2)/((float)600/16);
    else
        i = (y()-largeur/2)/((float)600/16);


    //que contient la case ?
    switch (MyGameScene::getCase(i,j)){

    case 1: {//N
        this->setPos(x(),y()-2*vitesse);
        break;
    }
    case 2: {//S
        this->setPos(x(),y()+2*vitesse);
        break;
    }
    case 4: {//E
        this->setPos(x()+2*vitesse,y());
        break;
    }
    case 5: { //NE
        if(MyGameScene::getCase(i-1,j)==1||MyGameScene::getCase(i-1,j)==5||MyGameScene::getCase(i-1,j)==9){ //N
            if(rotation()==-90)
                setY(y()-2*vitesse);
            else
            {
                setRotation(-90);
                this->setPos(j*600.0/16-(taille-1)*600.0/16/2,i*600.0/16+taille*600.0/16/2);
            }
        }
        else{ //E
            if(rotation()==0)
                setX(x()+2*vitesse);
            else
            {
                this->setPos((j+1)*(600.0/16)-taille*600.0/16/2,i*600.0/16-(taille-1)*600.0/16/2);
                setRotation(0);
            }
        }
        break;
    }
    case 6:{//SE
        if(MyGameScene::getCase(i-1,j)==6||MyGameScene::getCase(i-1,j)==2||MyGameScene::getCase(i-1,j)==10){//E
            if(rotation()==0)
                setX(x()+2*vitesse);
            else
            {
                setRotation(0);
                this->setPos((j+1)*(600.0/16)-taille*600.0/16/2,i*600.0/16-(taille-1)*600.0/16/2);
            }
        }
        else{//S
            if(rotation()==90)
                setY(y()+2*vitesse);
            else
            {
                this->setPos((j+1)*600.0/16+(taille-1)*600.0/16/2,(i+1)*600.0/16-taille*600.0/16/2);
                setRotation(90);
            }
        }
        break;
    }
    case 8: {//O
        this->setPos(x()-2*vitesse,y());
        break;
    }
    case 9: { //NO
        if(MyGameScene::getCase(i-1,j)==1||MyGameScene::getCase(i-1,j)==9||MyGameScene::getCase(i-1,j)==5){//N
            if(rotation()==-90){
                setY(y()-2*vitesse);
            }
            else
            {
                setRotation(-90);
                this->setPos(j*600.0/16-(taille-1)*600.0/16/2,i*600.0/16+taille*600.0/16/2);
            }
        }
        else{//O
            if(rotation()==180)
                setX(x()-2*vitesse);
            else
            {
                setRotation(180);
                this->setPos(j*600.0/16+taille*600.0/16/2,(i+1)*600.0/16+(taille-1)*600.0/16/2);
            }
        }
        break;
    }
    case 10: { //SO
        if(MyGameScene::getCase(i-1,j)==2||MyGameScene::getCase(i-1,j)==6||MyGameScene::getCase(i-1,j)==10){//O
            if(rotation()==180)
                setX(x()-2*vitesse);
            else
            {
                setRotation(180);
                this->setPos(j*600.0/16+taille*600.0/16/2,(i+1)*600.0/16+(taille-1)*600.0/16/2);
            }
        }
        else{//S
            if(rotation()==90)
                setY(y()+2*vitesse);
            else
            {
                setRotation(90);
                this->setPos((j+1)*600.0/16+(taille-1)*600.0/16/2,(i+1)*600.0/16-taille*600.0/16/2);
            }
        }
        break;
    }
    case 17: {//N
        this->setPos(x(),y()-2*vitesse);
        break;
    }
    case 18: { //S
        this->setPos(x(),y()+2*vitesse);
        break;
    }

    case 20: {
        this->setPos(x()+2*vitesse,y());
        break;
    }
    case 21: {
        if(MyGameScene::getCase(i-1,j)==1||MyGameScene::getCase(i-1,j)==5||MyGameScene::getCase(i-1,j)==9){ //N
            if(rotation()==-90)
                setY(y()-2*vitesse);
            else
            {
                setRotation(-90);
                this->setPos(j*600.0/16-(taille-1)*600.0/16/2,i*600.0/16+taille*600.0/16/2);
            }
        }
        else{ //E
            if(rotation()==0)
                setX(x()+2*vitesse);
            else
            {
                this->setPos((j+1)*(600.0/16)-taille*600.0/16/2,i*600.0/16-(taille-1)*600.0/16/2);
                setRotation(0);
            }
        }
        break;
    }

    case 22:{
        if(MyGameScene::getCase(i-1,j)==6||MyGameScene::getCase(i-1,j)==2||MyGameScene::getCase(i-1,j)==10){//E
            if(rotation()==0)
                setX(x()+2*vitesse);
            else
            {
                setRotation(0);
                this->setPos((j+1)*(600.0/16)-taille*600.0/16/2,i*600.0/16-(taille-1)*600.0/16/2);
            }
        }
        else{//S
            if(rotation()==90)
                setY(y()+2*vitesse);
            else
            {
                this->setPos((j+1)*600.0/16+(taille-1)*600.0/16/2,(i+1)*600.0/16-taille*600.0/16/2);
                setRotation(90);
            }
        }
        break;
    }

    case 24: {
        this->setPos(x()-2*vitesse,y());
        break;
    }

    case 25: {
        if(MyGameScene::getCase(i-1,j)==1||MyGameScene::getCase(i-1,j)==9||MyGameScene::getCase(i-1,j)==5){//N
            if(rotation()==-90){
                setY(y()-2*vitesse);
            }
            else
            {
                setRotation(-90);
                this->setPos(j*600.0/16-(taille-1)*600.0/16/2,i*600.0/16+taille*600.0/16/2);
            }
        }
        else{//O
            if(rotation()==180)
                setX(x()-2*vitesse);
            else
            {
                setRotation(180);
                this->setPos(j*600.0/16+taille*600.0/16/2,(i+1)*600.0/16+(taille-1)*600.0/16/2);
            }
        }
        break;
    }

    case 26: {
        if(MyGameScene::getCase(i-1,j)==2||MyGameScene::getCase(i-1,j)==6||MyGameScene::getCase(i-1,j)==10){//O
            if(rotation()==180)
                setX(x()-2*vitesse);
            else
            {
                setRotation(180);
                this->setPos(j*600.0/16+taille*600.0/16/2,(i+1)*600.0/16+(taille-1)*600.0/16/2);
            }
        }
        else{//S
            if(rotation()==90)
                setY(y()+2*vitesse);
            else
            {
                setRotation(90);
                this->setPos((j+1)*600.0/16+(taille-1)*600.0/16/2,(i+1)*600.0/16-taille*600.0/16/2);
            }
        }
        break;
    }
    case 32: {
        arrive=true;
    }
    }
}

